// import { ModelType, BaseOptionType, DateShortcutsKeyType, DateRangeOptionType } from './form-type';
import { parseTime } from '@/utils/common';
import { ref, getCurrentInstance } from 'vue';
export default function useDate(formModel: FormModelType, dateRange: any) {
	const instance = getCurrentInstance();
	const proxy = instance?.proxy;
	// 快捷选项的key
	const shortcutsKeys: DateShortcutsKeyType[] = ['last1h', 'today', 'lastDay', 'lastThreeDay', 'lastWeek'];

	// // 禁选日期
	// const disabledDate = (time) => {
	// 	return time.getTime() > Date.now();
	// };

	// 日期快捷选项枚举
	const shortcutsAllOptions = [
		{
			key: 'last10m',
			text: '最近十分钟',
			value() {
				const result = getTime('last10m');
				return result;
			},
		},
		{
			text: '最近一小时',
			key: 'last1h',
			value() {
				const result = getTime('last1h');
				return result;
			},
		},
		{
			text: '今天',
			key: 'today',
			value() {
				const result = getTime('today');
				return result;
			},
		},
		{
			text: '昨天',
			key: 'lastDay',
			value() {
				const result = getTime('lastDay');
				return result;
			},
		},
		{
			text: '最近一周',
			key: 'lastWeek',
			value() {
				const result = getTime('lastWeek');
				return result;
			},
		},
	];
	type shortcutsOptionsType = typeof shortcutsAllOptions;

	const shortcutsOptions = ref<shortcutsOptionsType>([]);

	// 显示的日期快捷选项
	const shortcutsOptionsFilter = (keyArr: Array<DateShortcutsKeyType> | undefined) => {
		// console.log('shortcutsOptionsFilter-');
		const result: shortcutsOptionsType = [];
		const dateShortcutsKey = keyArr || shortcutsKeys;
		shortcutsAllOptions.forEach((item) => {
			if (dateShortcutsKey.indexOf(item.key as DateShortcutsKeyType) > -1) {
				result.push(item);
			}
		});
		shortcutsOptions.value = result;
		// console.log('shortcutsOptionsFilter-', result);
		return result;
	};
	// 初始化日期组件的值
	/**
	 *
	 * @param option 时间选择器的选项
	 * @param value 时间选择器的值
	 */
	function initDateValue(option: BaseOptionType, value?: any[]) {
		if (value) {
			// console.log('initDateValue', value);
			dateRange.value = value;
			formModel.value.startTime = value[0];
			formModel.value.endTime = value[1];
		} else {
			// 日期默认值 没有设置默认的值的话就默认最近一天
			const defaultValue = option.defaulShortcutsKey || shortcutsKeys[0];
			if (shortcutsKeys.indexOf(defaultValue) > -1) {
				const arr = getTime(defaultValue);
				if (arr instanceof Array && arr.length === 2) {
					dateRange.value = arr;
					formModel.value.startTime = arr[0];
					formModel.value.endTime = arr[1];
				}
				// console.log('initDateValue', arr);
			}
		}
	}

	// 获取shortcut的时间
	function getTime(key: string) {
		const currentDate = new Date();
		const currentDateStr = parseTime(currentDate, '{y}-{m}-{d}');
		const currentDateTimeStr = parseTime(currentDate, '{y}-{m}-{d} {h}:{i}:00');
		let result;
		switch (key) {
			case 'last10m': // 最近10分钟
				result = [parseTime(currentDate.getTime() - 600000, '{y}-{m}-{d} {h}:{i}:00'), currentDateTimeStr];
				break;
			case 'last1h': // 最近一小时
				result = [parseTime(currentDate.getTime() - 3600000, '{y}-{m}-{d} {h}:{i}:00'), currentDateTimeStr];
				break;
			case 'today': // 今天
				result = [currentDateStr + ' 00:00:00', currentDateTimeStr];
				break;
			case 'lastDay': // 昨天
				result = [parseTime(currentDate.getTime() - 86400000, '{y}-{m}-{d}') + ' 00:00:00', currentDateStr + ' 00:00:00'];
				break;
			case 'lastThreeDay': // 最近三天
				result = [parseTime(currentDate.getTime() - 172800000, '{y}-{m}-{d}') + ' 00:00:00', currentDateTimeStr];
				break;
			case 'lastWeek': // 最近一周
				result = [parseTime(currentDate.getTime() - 604800000, '{y}-{m}-{d}') + ' 00:00:00', currentDateStr + ' 00:00:00'];
				break;
			// case 'empty': // 默认为空
			// 	result = ['', ''];
			// 	break;
		}
		// console.log('gettime', result);
		return result;
	}

	// 时间改变合并表单对象
	function timeRangeValueChange(option: BaseOptionType) {
		const timeRange: Array<any> = dateRange.value;
		if (option.enableCrossDay !== false) {
			// 判断是否跨周(最长一周)
			let crossWeekFlag = false;
			if (timeRange) {
				const startTimeUsec = new Date(timeRange[0]).getTime();
				const endTimeUsec = new Date(timeRange[1]).getTime();
				if (endTimeUsec > startTimeUsec + 24 * 60 * 60 * 1000 * 7) {
					crossWeekFlag = true;
				}
				if (crossWeekFlag && option.enableCrossWeek !== true) {
					proxy?.$message.warning('当前系统暂不支持跨周查询！');
					// formModel.value[option.prop] = [
					// 	parseTime(timeRange, '{y}-{m}-{d} 00:00:00'),
					// 	parseTime(startTimeUsec + 24 * 60 * 60 * 1000 * 7, '{y}-{m}-{d} 00:00:00'),
					// ];
					dateRange.value = [
						parseTime(timeRange, '{y}-{m}-{d} 00:00:00'),
						parseTime(startTimeUsec + 24 * 60 * 60 * 1000 * 7, '{y}-{m}-{d} 00:00:00'),
					];
					// changeTimeValue()
				}
				formModel.value.startTime = timeRange[0];
				formModel.value.endTime = timeRange[1];
			} else {
				formModel.value.startTime = '';
				formModel.value.endTime = '';
			}
		} else {
			// 不能跨天
			const minD = new Date(timeRange[0]);

			if (minD) {
				const today = new Date();
				const maxDTime = minD.getTime() + 24 * 60 * 60 * 1000;
				if (maxDTime > today.getTime()) {
					// 如果比当前时间大，则取当前时间
					dateRange.value = [parseTime(minD, '{y}-{m}-{d} 00:00:00'), parseTime(today, '{y}-{m}-{d} {h}:{i}:00')];
				} else {
					dateRange.value = [parseTime(minD, '{y}-{m}-{d} {h}:{i}:00'), parseTime(new Date(maxDTime), '{y}-{m}-{d} {h}:{i}:00')];
				}
				formModel.value.startTime = timeRange[0];
				formModel.value.endTime = timeRange[1];
			}
		}
		console.log('timeRangeValueChange', formModel.value);
	}
	return {
		shortcutsOptions,
		shortcutsOptionsFilter,
		initDateValue,
		timeRangeValueChange,
	};
}
